package com.yjt.fresh.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.widget.LinearLayout;

import com.yjt.fresh.R;

/**
 *
 *
 * @author mcc
 * @date 2018-06-06
 */
public class CouponView extends LinearLayout {

    private Paint mPaint;
    private Context mContext;
    private int mRadius;
    private String mOrientation;

    private static final String UP = "up";
    private static final String DOWN = "down";
    private static final int DEFAULT_RADIUS = 10;

    public CouponView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CouponView, defStyleAttr, 0);
        mRadius = a.getDimensionPixelSize(R.styleable.CouponView_radius, dp2Px(DEFAULT_RADIUS));
        mOrientation = a.getString(R.styleable.CouponView_orientations);
        a.recycle();
        init();
    }

    public CouponView(Context context) {
        this(context, null);
    }

    public CouponView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }
    private void init() {
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setDither(true);
        mPaint.setColor(Color.WHITE);
        mPaint.setStyle(Paint.Style.FILL);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (DOWN.equals(mOrientation)) {
            canvas.drawCircle(0,getMeasuredHeight(), mRadius,mPaint);
            canvas.drawCircle(getMeasuredWidth(),getMeasuredHeight(),mRadius,mPaint);
        }else if (UP.equals(mOrientation)){
            canvas.drawCircle(0,0, mRadius,mPaint);
            canvas.drawCircle(getMeasuredWidth(),0,mRadius,mPaint);
        }
    }

    private int dp2Px(float dp) {
        return (int) (dp * mContext.getResources().getDisplayMetrics().density + 0.5f);
    }
}
